//
//  JJLNetWorkManager.m
//  Code
//
//  Created by LEE on 2017/11/14.
//  Copyright © 2017年 HD. All rights reserved.
//

#import "JJLNetWorkManager.h"

@implementation JJLNetWorkManager
#pragma mark - 判断是否有网络

+ (BOOL)internetAvailable{
    
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    [mgr startMonitoring];
    return mgr.reachable;
    
}

#pragma mark - 发起请求

+(void)getData:(NSString *)url params:(NSMutableDictionary *)params successWith200:(successWith200)successWith200 successWithOther:(successWithOther)successWithOther fail:(fail)fail{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
//    [SVProgressHUD show];
    
    NSMutableDictionary *params1 = params;
    
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy = securityPolicy;
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/json",
                                                          @"text/javascript", @"text/html",
                                                          @"application/xml", @"text/plain",nil];
    
    [manager POST:url  parameters:params1 progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        
        if ([responseObject[@"status"] intValue] == 200) {
//            [SVProgressHUD dismiss];
            successWith200(responseObject);
            return;
        }else {
//            [SVProgressHUD showImage:nil status:[NSString stringWithFormat:@"%@",responseObject[@"message"]]];
            [self performSelector:@selector(dismiss) withObject:nil afterDelay:1.5];
            successWithOther(responseObject);
            return;
        }
        
    }failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
//        [SVProgressHUD showImage:nil status:@"请求失败"];
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:1.5];
        fail(error);

        return ;
        
    }];
}


+(void)getData:(NSString *)url params:(NSMutableDictionary *)params successWith200:(successWith200)successWith200 fail:(fail)fail{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
//    [SVProgressHUD show];
    
    NSMutableDictionary *params1 = params;
   
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy = securityPolicy;
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/json",
                                                          @"text/javascript", @"text/html",
                                                          @"application/xml",@"text/plain", nil];
    
    [ manager POST:url parameters:params1 progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
//        [SVProgressHUD dismiss];
        
        if ([responseObject[@"status"] intValue] == 200) {
            successWith200(responseObject);
            return;
        }else {
//            [SVProgressHUD showImage:nil status:[NSString stringWithFormat:@"%@",responseObject[@"message"]]];
            [self performSelector:@selector(dismiss) withObject:nil afterDelay:1.5];
            return;
        }
    }failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
//        [SVProgressHUD showImage:nil status:@"请求失败"];
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:1.5];
        fail(error);
        return;
    }];
}





+ (void)dismiss{
//    [SVProgressHUD dismiss];
}

@end
